<template>
  <div style="width: 100%" v-if="visible">
    <el-form ref="form" :model="form" :rules="rules" label-position="top" size="mini">
      <el-row>
        <el-col :span="24">
          <el-form-item label="机器" prop="machineId">
            <el-select v-model="form.machineId" placeholder="请选择机器" clearable :style="{width: '100%'}">
              <el-option v-for="(item, index) in machineList" :key="index" :label="item.label" :value="item.value"
                         :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="代码仓" prop="vcsId">
            <el-select @change="getBranchList" v-model="form.vcsId" placeholder="请选择代码仓" clearable :style="{width: '100%'}">
              <el-option v-for="(item, index) in vcsList" :key="index" :label="item.label" :value="item.value"
                         :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="selectBranch">
          <el-form-item label="触发分支" prop="branch">
            <el-select v-model="form.branch" placeholder="请选择触发分支" clearable :style="{width: '100%'}">
              <el-option v-for="(item, index) in branchList" :key="index" :label="item.label" :value="item.value"
                         :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="selectBranch">
          <el-form-item label="历史版本" prop="version">
            <el-select v-model="form.version" placeholder="请选择历史版本" clearable :style="{width: '100%'}">
              <el-option v-for="(item, index) in versionList" :key="index" :label="item.label" :value="item.value"
                         :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="代码存放路径" prop="path">
                <span slot="label">
                  代码存放路径
                  <el-tooltip placement="top">
                    <div slot="content">
                      代码存放路径示例：target/devops-platform 多个路径使用;分号分隔
                    </div>
                    <i class="el-icon-question"/>
                  </el-tooltip>
                </span>
            <el-input v-model="form.path" placeholder="请输入代码存放路径"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" style="width: 100%" type="primary" @click="submitForm">保 存</el-button>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import Element from 'element-ui'
  import 'element-ui/lib/theme-chalk/index.css';
  import {addNodeInfo, nodeInfo, updateInfo} from '@/lib/api-pipeline';

  Vue.use(Element)
  export default {
    name: "pipeline-start",
    data() {
      return {
        id:0,
        visible: false,
        form: {
          machineId: '',
          vcsId: '',
          branch: '',
          path: '',
          commitId: ''
        },
        machineList: [],
        vcsList: [],
        branchList: [],
        versionList: [],
        rules: {
          machineId: [{required: true, message: '机器不能为空', trigger: 'blur'}],
          vcsId: [{required: true, message: '代码仓不能为空', trigger: 'blur'}],
          branch: [{required: true, message: '触发分支不能为空', trigger: 'blur'}],
          version: [{required: true, message: '历史版本不能为空', trigger: 'blur'}],
          path: [{required: true, message: '代码存放路径不能为空', trigger: 'blur'}],
        }
      }
    },

    computed: {
      selectBranch: function() {
        return Boolean(this.form.vcsId)
      }
    },

    methods: {
      init(nodeUuid) {
        //组件数据初始化，防止出现使用同一个组件的两个节点之间数据关联
        Object.assign(this.$data, this.$options.data())
        this.nodeUuid = nodeUuid
        nodeInfo({'nodeUuid': nodeUuid}).then(response => {
          if(response.code === 200) {
            const data = response.data
            if (data) {
              this.form = JSON.parse(data.info)
              this.id = data.id
            }
            if(this.form.vcsId) {
              this.getBranchList(this.form.vcsId);
            }
          }
          else {
            this.msgError(response.msg)
          }
          this.visible = true
        }).catch(err => {
          console.log('err')
        })
        this.getMachineList()
        this.getVcsList()
      },

      getMachineList() {
        this.$api.getMachineList({
          page: 1,
          limit: 100
        }).then(({ data }) => {
          this.machineList = data.value.map(item => {
            return {
              value: item.id,
              label: item.name
            }
          })
        }).catch(() => {
          console.log('err')
        })
      },

      getVcsList() {
        this.$api.getVcsList({
          page: 1,
          limit: 100
        }).then(({ data }) => {
          this.vcsList = data.value.map(item => {
            return {
              value: item.id,
              label: item.name
            }
          })
        }).catch(() => {
          console.log('err')
        })
      },

      getBranchList(value) {
        if(value) {
          this.$api.getVcsInfo({
            id: value
          }).then(({ data }) => {
            this.branchList = data.branches.map(item => {
              return {
                value: item.name,
                label: item.name
              }
            })
            this.versionList = data.commits.map(item => {
              return {
                value: item.id,
                label: item.message
              }
            })
          }).catch(() => {
            console.log('err')
          })
        }
      },

      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            // 构建数据json
            let info = {
              'id': this.id,
              'nodeUuid': this.nodeUuid,
              'nodeName': '开始',
              'info': JSON.stringify(this.form)
            }
            if (this.id !== 0) {
              updateInfo({'pipelineNodeInfoDTO': info}).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addNodeInfo({'pipelineNodeInfoDTO': info}).then(response => {
                if (response.code === 200) {
                  this.id = response.data //更新新增节点id
                  this.msgSuccess('新增成功')
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      },
    },

  }
</script>

<style scoped>

</style>
